#!/bin/bash

echo "Testing EbbFlow WaterBear + Epoch Management (No ADKR)"
echo "====================================================="
echo "Test Configuration:"
echo "  - Nodes: 4"
echo "  - Duration: 30 seconds"
echo "  - Components: WaterBear + Epoch Management (No ADKR)"
echo ""

# Clean up any existing processes
pkill -f waterbear_epoch_server 2>/dev/null || true
pkill -f waterbear_only_server 2>/dev/null || true
pkill -f ebbflow 2>/dev/null || true
sleep 2

# Create logs directory
mkdir -p logs

echo "=== Testing WaterBear + Epoch Management ==="

# Start WaterBear + Epoch servers
echo "Starting WaterBear + Epoch servers..."
for i in {0..3}; do
    echo "Started WaterBear + Epoch server $i"
    ./bin/waterbear_epoch_server -id $i > logs/waterbear_epoch_server_${i}.log 2>&1 &
    sleep 1
done

# Wait for servers to start
sleep 3

# Start client
echo "Starting EbbFlow client..."
./bin/ebbflow_client -batch 100 -count 1000 -txsize 250 > logs/waterbear_epoch_client.log 2>&1 &

# Wait for test to complete
echo "Running WaterBear + Epoch test for 30 seconds..."
sleep 35

# Stop processes
echo "Stopping WaterBear + Epoch processes..."
pkill -f waterbear_epoch_server 2>/dev/null || true
pkill -f ebbflow_client 2>/dev/null || true
sleep 2

echo ""
echo "=== WaterBear + Epoch Performance Analysis ==="

# Analyze server logs
echo "Server Logs Analysis:"
echo "--------------------"
for i in {0..3}; do
    if [ -f "logs/waterbear_epoch_server_${i}.log" ]; then
        echo "Server $i:"
        
        # Count activities
        waterbear_count=$(grep -c "WaterBear" logs/waterbear_epoch_server_${i}.log 2>/dev/null || echo "0")
        epoch_count=$(grep -c "Epoch" logs/waterbear_epoch_server_${i}.log 2>/dev/null || echo "0")
        acs_count=$(grep -c "ACS" logs/waterbear_epoch_server_${i}.log 2>/dev/null || echo "0")
        adkr_count=$(grep -c "ADKR" logs/waterbear_epoch_server_${i}.log 2>/dev/null || echo "0")
        crash_count=$(grep -c "panic" logs/waterbear_epoch_server_${i}.log 2>/dev/null || echo "0")
        
        echo "  - WaterBear Activities: $waterbear_count"
        echo "  - Epoch Activities: $epoch_count"
        echo "  - ACS Activities: $acs_count"
        echo "  - ADKR Activities: $adkr_count"
        echo "  - Crashes: $crash_count"
        
        # Check for specific activities
        if grep -q "WaterBear initialized" logs/waterbear_epoch_server_${i}.log 2>/dev/null; then
            echo "  - WaterBear Initialized: Yes"
        else
            echo "  - WaterBear Initialized: No"
        fi
        
        if grep -q "Epoch Management initialized" logs/waterbear_epoch_server_${i}.log 2>/dev/null; then
            echo "  - Epoch Management Initialized: Yes"
        else
            echo "  - Epoch Management Initialized: No"
        fi
        
        if grep -q "Epoch management completed" logs/waterbear_epoch_server_${i}.log 2>/dev/null; then
            echo "  - Epoch Management Completed: Yes"
        else
            echo "  - Epoch Management Completed: No"
        fi
        
        echo ""
    fi
done

# Analyze client logs
if [ -f "logs/waterbear_epoch_client.log" ]; then
    echo "Client Logs Analysis:"
    echo "-------------------"
    
    # Extract transaction info
    tx_submitted=$(grep "Submitted:" logs/waterbear_epoch_client.log | tail -1 | grep -o '[0-9]*' | head -1 || echo "0")
    tx_time=$(grep "Time:" logs/waterbear_epoch_client.log | tail -1 | grep -o '[0-9.]*' | head -1 || echo "0")
    
    echo "  - Transactions Submitted: $tx_submitted"
    echo "  - Submission Time: $tx_time seconds"
    
    if [ "$tx_time" != "0" ] && [ "$tx_time" != "" ]; then
        tps=$(echo "scale=2; $tx_submitted / $tx_time" | bc 2>/dev/null || echo "0")
        echo "  - WaterBear + Epoch TPS: $tps"
    else
        echo "  - WaterBear + Epoch TPS: Unable to calculate"
    fi
    
    echo ""
fi

# Check for crashes
total_crashes=$(find logs -name "*waterbear_epoch*" -exec grep -l "panic" {} \; 2>/dev/null | wc -l)
echo "=== WaterBear + Epoch Test Results ==="
echo "Total Server Crashes: $total_crashes"

if [ "$total_crashes" -eq 0 ]; then
    echo "✅ WaterBear + Epoch Management runs successfully without crashes!"
else
    echo "❌ WaterBear + Epoch Management still has crashes"
    echo "Latest crash details:"
    find logs -name "*waterbear_epoch*" -exec grep -l "panic" {} \; 2>/dev/null | head -1 | xargs tail -10
fi

echo ""
echo "WaterBear + Epoch test completed."


